//
//  ZpwelcomeViewController.m
//  WeiBo_OC
//
//  Created by MacBook Pro on 16/5/29.
//  Copyright © 2016年 MacBook Pro. All rights reserved.
//

#import "ZpwelcomeViewController.h"
#import "ZpUserAccountViewModel.h"

@interface ZpwelcomeViewController ()

@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) ZpUserAccountViewModel *userAccountViewModel;
@end

@implementation ZpwelcomeViewController

- (void)loadView{
    UIImageView * imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    self.view = imgView;
    self.backView = imgView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//     UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
//    [self.view addSubview:imgV];
    self.userAccountViewModel = [ZpUserAccountViewModel shareUserAccountViewModel];
    [self setUI];
}

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    
    [self startAnimation];
}
- (void)setUI{
    NSURL *url = [NSURL URLWithString:self.userAccountViewModel.userAccount.avatar_large];
    [self.iconView sd_setImageWithURL:url];
    self.nameLabel.text = self.userAccountViewModel.userAccount.name;
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.nameLabel];
    
    [self.iconView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-200);
        make.centerX.equalTo(self.view);
        make.width.equalTo(90);
        make.height.equalTo(90);
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.bottom).offset(8);
        make.centerX.equalTo(self.view);
        make.width.equalTo(80);
        make.height.equalTo(40);
    }];
}

- (void)startAnimation{

    
    [self.iconView updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(200 - [UIScreen mainScreen].bounds.size.height);
    }];
    
    [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:6 options:0 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.nameLabel.alpha = 1;
        } completion:^(BOOL finished) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"SwitchRootViewController" object:@"main"];
            });
 
        }];
        

    }];
}

- (UIImageView *)iconView{
    if (!_iconView) {
        UIImage *icon = [UIImage imageNamed:@"avatar_default_big"];
        _iconView = [[UIImageView alloc] initWithImage:icon];
        _iconView.layer.cornerRadius = icon.size.width /  icon.scale ;
        NSLog(@"%f",icon.size.width );
        _iconView.clipsToBounds = YES;
    }
    return _iconView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.alpha = 0;
        _nameLabel.textColor = [UIColor darkGrayColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;

}



@end
